<?php

/**
 * Block Model
 * @author phuongtn
 */
class block
{
    /**
     * @var system_model Singleton instance
     */
    protected static $_instance;

    /**
     * Avoid directly create instance
     */
    private function __construct()
    {

    }

    /**
     * Retrieve singleton instance
     *
     * @return system_model
     */
    public static function getInstance()
    {
        if (null === self::$_instance)
        {
            self::$_instance = new self();
        }
        return self::$_instance;
    }

    /**
     * Reset the singleton instance
     *
     * @return void
     */
    public static function resetInstance()
    {
        self::$_instance = null;
    }

    /**
     * Add block
     * @param type $params
     * @return type
     * @throws Core_Exception 
     */
    public function addBlock($params, $redisOnly = false)
    {
        try
        {
            if($redisOnly == true)
            {
                $insertRS = true;
                $cacheFormat = $params;
              
            }
            else
            {
                //init model metadata
                $blockMeta = new Fpt_Business_Block_block_metadata_block();
                //init data
                $blockMeta->init($params);
                //get formatted data
                $formatParams = $blockMeta->getData();

                //unsert old
                unset($params);

                
                //init model db
                $modelSql = Fpt_Business_Block_layout_adapter_mysql::getInstance();
                //add db block
                $insertRS = $modelSql->addBlock($formatParams);
                
                $cacheFormat = $blockMeta->getDataCache();
            }
            
            
            //if insert db success;
            if($insertRS !== false)
            {
                //init model redis
                $blockRedis = Fpt_Business_Block_block_adapter_redis::getInstance();
                
               
                $redis =  $blockRedis->addBlock($cacheFormat);
                
                if($redis != false)
                {
                    return $cacheFormat['BlockName'];
                }
            }
            return false;
        }
        catch(Exception $ex)
        {
            throw new Core_Exception($ex->getMessage(), $ex->getCode());
        }
        return false;
    }
    
    public function getBlock($blockName, $status = 1)
    {
        try
        {
            //init model redis
            $blockRedis = Fpt_Business_Block_block_adapter_redis::getInstance();
            //init params
            $getParams = array('BlockName' => $blockName, 'BlockStatus' => $status);
            //get redis detail
            $blockDetail =  $blockRedis->getBlock($getParams);
            
            if(empty($blockDetail))
            {
               
                //init model db
                $modelSql = Fpt_Business_Block_block_adapter_mysql::getInstance();
                
                //add db block
                $blockDetail = $modelSql->getBlock($getParams);
                $blockDetail = $blockDetail[0];
                //if insert db success;
                if($blockDetail !== false)
                {
                     $blockRedis->addBlock($blockDetail);
                }
                
            }
            //decode 
            if(isset($blockDetail['BlockParams']))
            {
                $blockDetail['BlockParams'] = Zend_Json::decode($blockDetail['BlockParams']);
            }
            return $blockDetail;
        }
        catch(Exception $ex)
        {
            throw new Core_Exception($ex->getMessage(), $ex->getCode());
        }
    }
    
   /**
     * Get list block
     * @return type
     * @throws Core_Exception 
     */
    public function getListBlock()
    {
        try
        {
            $response = false;
            //init model redis
            $blockRedis = Fpt_Business_Block_block_adapter_redis::getInstance();
          
            //get redis detail
            $blockList =  $blockRedis->getListBlock();
            
            //check get list sucess
            if(empty($blockList))
            {
                
                //init model db
                $modelSql  = Fpt_Business_Block_block_adapter_mysql::getInstance();
                
                //get list block
                $blockListDB = $modelSql->getListBlock();
                
                if($blockListDB !== false)
                {
                   
                    foreach($blockListDB['data'] as $key => $detail)
                    {
                        $blockList[] = $detail['BlockName'];
                        $this->addBlock($detail, true);
                    }
                }
  
            }
            
            //get block detial
            if(is_array($blockList))
            {
                foreach($blockList as $blockName)
                {
                    $response[] = $this->getBlock($blockName);
                }
            }
            
            return $response;
            
            
           
        }
        catch(Exception $ex)
        {
            throw new Core_Exception($ex->getMessage(), $ex->getCode());
        }
        return $response;
    }
    
    
}